home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / PAINT.CPP < prev    next >
C/C++ Source or Header  |  1992-11-21  |  4KB  |  192 lines

  1. // ---------- paint.cpp
  2.  
  3. #include "desktop.h"
  4. #include "dfwindow.h"
  5.  
  6. void DFWindow::PaintOverLappers()
  7. {
  8.     Rect src = ShadowedRect();
  9.     DFWindow *Wnd = parent->first;
  10.     while (Wnd != NULL)    {
  11.         if (Wnd->visible && Wnd != this)    {
  12.             Rect rc = Wnd->ShadowedRect();
  13.             rc = rc.subRectangle(src);
  14.             if (rc.ValidRectangle())    {
  15.                 Wnd->clipoverride = True;
  16.                 Wnd->Show();
  17.                 Wnd->clipoverride = False;
  18.             }
  19.         }
  20.         Wnd = Wnd->next;
  21.     }
  22. }
  23.  
  24. void DFWindow::ClearWindow()
  25. {
  26.     String ln(ClientWidth(), clearch);
  27.  
  28.     int h = ClientHeight();
  29.     for (int y = 0; y < h; y++)
  30.         WriteClientString(ln, 0, y, colors.fg, colors.bg);
  31. }
  32.  
  33. void DFWindow::Paint(Rect rc)
  34. {
  35.     if (visible)    {
  36.         String ln(rc.Width(), clearch);
  37.         int x = rc.Left()-ClientLeft();
  38.         int y = rc.Top()-ClientTop();
  39.         clipoverride = True;
  40.         for (int i = 0; i < rc.Height(); i++)
  41.             WriteClientString(ln, x, y+i, colors.fg, colors.bg);
  42.         clipoverride = False;
  43.     }
  44. }
  45.  
  46. void DFWindow::Paint()
  47. {
  48.     if (visible)
  49.         ClearWindow();
  50. }
  51.  
  52. static Bool ClipRectangle(Rect &rc, String &ln, int x, int y)
  53. {
  54.     if (y < rc.Top() || y > rc.Bottom())
  55.         return True;
  56.     if (x + ln.Strlen() <= rc.Left())
  57.         return True;
  58.     if (x > rc.Right())
  59.         return True;
  60.     if (x < rc.Left())    {
  61.         ln = ln.left(rc.Left() - x);
  62.         x = rc.Left();
  63.     }
  64.     int len = ln.Strlen();
  65.     if (x+len-1 > rc.Right())
  66.         ln[rc.Right()-x+1] = '\0';
  67.     return False;
  68. }
  69.  
  70. static Bool ClipChar(Rect &rc, int x, int y)
  71. {
  72.     if (y < rc.Top() || y > rc.Bottom())
  73.         return True;
  74.     if (x < rc.Left() || x > rc.Right())
  75.         return True;
  76.     return False;
  77. }
  78.  
  79. Bool DFWindow::ClipParent(int &x, int y, String *ln = NULL)
  80. {
  81.     if (!(attrib & NOCLIP))    {
  82.         DFWindow *Wnd = parent;
  83.         Rect rc;
  84.         if (attrib & FRAMEWND)
  85.             rc = Wnd->rect;
  86.         else
  87.             rc = Wnd->ClientRect();
  88.         while (Wnd != NULL)    {
  89.             if (ln != NULL)    {
  90.                 if (ClipRectangle(rc, *ln, x, y))
  91.                     return True;
  92.             }
  93.             else
  94.                 if (ClipChar(rc, x, y))
  95.                     return True;
  96.             if ((Wnd = Wnd->parent) != NULL)
  97.                 rc = Wnd->ClientRect();
  98.         }
  99.     }
  100.     return False;
  101. }
  102.  
  103. void DFWindow::WriteString(String &ln, int x, int y, Rect &rc, int fg, int bg)
  104. {
  105.     if (this == desktop.InFocus() || clipoverride)    {
  106.         if (!ClipRectangle(rc, ln, x, y))
  107.             if (!ClipParent(x, y, &ln))
  108.                 desktop.screen().WriteVideoString(ln, x, y, fg, bg);
  109.     }
  110.     else     {
  111.         char *cp = ln;
  112.         while (*cp)
  113.             WriteChar(*cp++, x++, y, rc, fg, bg);
  114.     }
  115. }
  116.  
  117. void DFWindow::WriteChar(int ch, int x, int y, Rect &rc, int fg, int bg)
  118. {
  119.     if (this != desktop.InFocus() && !clipoverride)
  120.         if (this != inWindow(x, y, fg, bg))
  121.             return;
  122.     if (ClipChar(rc, x, y))
  123.         return;
  124.     if (ClipParent(x, y))
  125.         return;
  126.     desktop.screen().PutVideoChar(x, y, (ch & 255) | (clr(fg,bg) << 8));
  127. }
  128.  
  129. inline Bool inShadow(Rect &rc, int x, int y)
  130. {
  131.     return (Bool)
  132.     (((x == rc.Right()+1) && (y > rc.Top() && y < rc.Bottom()+2))
  133.                             ||
  134.     ((y == rc.Bottom()+1) && (x > rc.Left() && x < rc.Right()+2)));
  135. }
  136.  
  137. // ---- find the window that coordinates are in
  138. DFWindow *inWindow(int x, int y, int &fg, int &bg)
  139. {
  140.     DFWindow *Hit = NULL;
  141.     DFWindow *Wnd = desktop.ApplWnd();
  142.     while (Wnd != NULL)    {
  143.         if (Wnd->visible)    {
  144.             Rect rc = Wnd->VisibleRect();
  145.             if (rc.Inside(x, y))    {
  146.                 Hit = Wnd;
  147.                 Wnd = Wnd->last;
  148.                 continue;
  149.             }
  150.             // --- in case the char is writing under a shadow
  151.             if ((fg || bg) && (Wnd->attrib & SHADOW))    {
  152.                 if (rc.Right() == Wnd->rect.Right() ||
  153.                         rc.Bottom() == Wnd->rect.Bottom())    {
  154.                     if (inShadow(rc, x, y))    {
  155.                         fg = ShadowFG;
  156.                         bg = ShadowBG;
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.         Wnd = Wnd->prev;
  162.     }
  163.     return Hit;
  164. }
  165.  
  166. Rect &DFWindow::VisibleRect()
  167. {
  168.     static Rect rc;
  169.     rc = rect;
  170.     if (!(attrib & NOCLIP))    {
  171.         DFWindow *Wnd = parent;
  172.         Rect prc;
  173.         if (attrib & FRAMEWND)
  174.             prc = Wnd->rect;
  175.         else
  176.             prc = Wnd->ClientRect();
  177.         while (Wnd != NULL)    {
  178.             if (Wnd->attrib & NOCLIP)
  179.                 break;
  180.             rc = rc.subRectangle(prc);
  181.             if (!rc.ValidRectangle())
  182.                 break;
  183.             if ((Wnd = Wnd->parent) != NULL)
  184.                 prc = Wnd->ClientRect();
  185.         }
  186.     }
  187.     return rc;
  188. }
  189.  
  190.  
  191.  
  192.